package deprecated.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import util.DomainUtils;
import util.FormArgumentsException;
import domain.dao.interfaces.IDAOContactGroup;

/**
 * Servlet implementation class RemoveFromContactGroupServlet
 */
public class RemoveFromContactGroupServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RemoveFromContactGroupServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		String[] contactsToRemove = request
				.getParameterValues("contacts_to_remove");
		String strIdContactGroup = request
				.getParameter("selected_contactgroup");
		int idContactGroup = -1;
		
		ApplicationContext context = util.SpringUtils.getApplicationContext();
		
		try {
			checks(request, contactsToRemove);
			idContactGroup = Integer.parseInt(strIdContactGroup);
			
			IDAOContactGroup daoContactGroup = (IDAOContactGroup) context.getBean("beanDAOContactGroup");
			daoContactGroup.removeContactsFromGroup(
					 idContactGroup, contactsToRemove);
//			DAOFactory.getDaoContactGroup().removeContactsFromGroup(
//					null, idContactGroup, contactsToRemove);
			
			DomainUtils.displayInfoMessage(request,
					DomainUtils.ADD_CONTACT_GROUP_ALERT_CURRENT_COLOR,
					"Specified contacts had been removed successfully.");
			
		} catch (Exception e) {

			if ((e instanceof FormArgumentsException) == false) {
				request.setAttribute(DomainUtils.ALERT_ERROR_STRING,
						"An error occur on the server.");
				System.err.println(e);
			}
			
			DomainUtils.keepParametersContent(request);
		}

		request.getRequestDispatcher("pages/removeFromContactGroup.jsp")
				.forward(request, response);
	}

	private void checks(HttpServletRequest request, String[] contactsToRemove)
			throws Exception {
		
		HttpSession session = request.getSession(false);
		// Checking session
		if (session == null
				|| session.getAttribute(DomainUtils.CURRENT_USER_STRING) == null) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING, 
					"You are not currently connected. May be the session expired.");
			throw new FormArgumentsException();
		}
		
		if (contactsToRemove == null || contactsToRemove.length == 0) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING,
					"You should select at least a contact.");
			throw new FormArgumentsException();
		}

	}
}
